import { WebManageConst } from "src/consts/web_manage_const";
import * as Api from 'src/api/net';
import { PopupName } from "src/consts/popup_const";

const state: ListMsgData = {} as any;
// StateIniter.init(state, 'demo');

state.curr_page = 1;
state.page_size = 15;
state.total_page = 1;
state.total_size = 1;
state.data_list = []
state.type = "";
// vuex

/** 更新数据的 */
const mutations = {
    [WebManageConst.CommitWebMList](state: ListMsgData, data: ListMsgData): void {
        console.log("WebM data update", data);
        state.curr_page = data.curr_page;
        state.page_size = data.page_size;
        state.total_page = data.total_page;
        state.total_size = data.total_size;
        let len = data.data_list.length;
        for (let i = 0; i < len; i++) {
            let tmp: DemoData = data.data_list[i];
            tmp.selected = false;
        }
        state.data_list = data.data_list;
    },

    [WebManageConst.CommitType](state: ListMsgData, data: String): void {
        state.type = data
    }
}

//异步处理
const actions = {
    [WebManageConst.DispatchWebMList](context: VuexData, data?: any): void {
        var authority = getUserInfo().authority_list
        var req = ""
        var flag = false
        for (let i = 0; i < authority.length; i++) {
            if (authority[i].name == "公共网站管理") {
                for (let j = 0; j < authority[i].child_list.length; j++) {
                    if (authority[i].child_list[j].name == "网站管理") {
                        for (let k = 0; k < authority[i].child_list[j].child_list.length; k++) {
                            if (authority[i].child_list[j].child_list[k].id == "ROLE_website_manage_admin_list") {
                                flag = true
                            }
                        }
                    }
                }
            }
        }

        if (flag) {
            req = "/website/manage/admin/list.do"
        } else {
            req = "/website/manage/normal/list.do"
        }

        Api.AjaxPost(Config.ajaxUrl + req, data, (res: any) => {
            if (isNoEmptyObject(res.data)) {
                WLCommit(WebManageConst.CommitWebMList, res.data);
            }
        })
    },
    [WebManageConst.DispatchWebMQiYong](context: VuexData, data?: any): void {
        let sendData: any = {};
        sendData["id"] = data.id
        sendData["web_status"] = data.web_status
        Api.AjaxPost(Config.ajaxUrl + "/website/status/edit.do", sendData, (res: any) => {
            if (data.flag) {
                if (data.type == "qy") {
                    WLMessage("网站启用成功", "success")
                } else if (data.type == "ty") {
                    WLMessage("网站停用成功", "success")
                }
                let senData: any = {}
                senData["curr_page"] = state.curr_page
                senData["page_size"] = state.page_size
                WLDispatch(WebManageConst.DispatchWebMList, senData);
            }
        })
    },
}


export default {
    state,
    mutations,
    actions
}